﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Easydp.Controllers;
using Easydp.Models.Entities;
using System.Net;
using MSXML2;
using System.Collections.Specialized;
using System.Text;
namespace Web.Views.AilPay
{
    public partial class AliPayReceive : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string OrderNumber = Request.QueryString["out_trade_no"];
            OrderController orderController = new OrderController();
            TT_Orders orderObj=orderController.GetModel(string.Format(" OrderNum = '{0}' ",OrderNumber));

            string alipayNotifyURL = "http://notify.alipay.com/trade/notify_query.do?";
            //string partner = "2088002061866935";                        //partner		合作伙伴ID			保留字段
            //string key = "btfhyepobkdkemcxtk2m74tmt85vjnzh";              //partner账户的支付宝安全校验码

            string partner = "2088002166377983";                        //partner		合作伙伴ID			保留字段
            string key = "zhiasv6quoplnykcii4vo3iv3afvh6si";           //partner     账户的支付宝安全校验码

            alipayNotifyURL = alipayNotifyURL + "service=notify_verify&partner=" + partner + "&notify_id=" + Request.QueryString["notify_id"];
            string responseTxt = AliPayCode.NotifyURL(alipayNotifyURL);

            //*********************************************************************************************
            int i;
            NameValueCollection coll;
            //Load Form variables into NameValueCollection variable.
            coll = Request.QueryString;

            // Get names of all forms into a string array.
            String[] requestarr = coll.AllKeys;


            //进行排序；
            string[] Sortedstr = AliPayCode.BubbleSort(requestarr);

            //构造待md5摘要字符串 ；
            StringBuilder prestr = new StringBuilder();

            for (i = 0; i < Sortedstr.Length; i++)
            {
                if (Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type")
                {
                    if (i == Sortedstr.Length - 1)
                    {
                        prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]]);

                    }
                    else
                    {
                        prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]] + "&");
                    }
                }
            }
            prestr.Append(key);
            //生成Md5摘要；
            string mysign = AliPayCode.GetMD5(prestr.ToString());
            string sign = Request.QueryString["sign"];

            // 返回的订单编号与实际支付金额

            string PayMoney = Request.QueryString["total_fee"].ToString();

            if (mysign == sign && responseTxt == "true")   //验证支付发过来的消息，签名是否正确
            {
                StringBuilder meg = new StringBuilder("\r\n收到支付结果:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ";订单号:" +OrderNumber + ";成功收到支付宝返回结果" );
         
                AliPayCode.WriteLog(meg.ToString(), LogType.AliPayReturn);
                //=== 支付成功
                //Response.Redirect("PayInsertDB.aspx?payInterface=支付宝&id=" + Order_Id[0] + "&money=" + PayMoney + "&notifyid=" + Request.QueryString["notify_id"] + "&buyerId=" + Request.QueryString["buyer_id"] + "&sellerId=" + Request.QueryString["seller_id"] + "&login=" + Order_Id[1]);
                Response.Redirect("SucceedPay.aspx?payInterface=支付宝&money=" + PayMoney + "&NetPayMent=true&DB=true");
            }
            else
            {
                //=== 支付失败
                Response.Redirect("SucceedPay.aspx?payInterface=支付宝&money=0&NetPayMent=false&DB=false&orderid=" + orderObj.OrderID + "");
            }
        }
    }
}
